package es.supermanager;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

import es.supermanager.ExtraerCodigos;

public class Test {

	private static Jugador[] bases;
	private static Jugador[] aleros;
	private static Jugador[] pivots;

	public static void main(String[] args) throws InterruptedException {

		do {
			
			System.out.println("Actualizando...");
			
			PartidoNuevoParser p;
			p = new PartidoNuevoParser(271);
			p.actualizar();
			p = new PartidoNuevoParser(272);
			p.actualizar();
			p = new PartidoNuevoParser(273);
			p.actualizar();
			p = new PartidoNuevoParser(274);
			p.actualizar();
			p = new PartidoNuevoParser(275);
			p.actualizar();
			p = new PartidoNuevoParser(276);
			p.actualizar();
			p = new PartidoNuevoParser(277);
			p.actualizar();
			p = new PartidoNuevoParser(278);
			p.actualizar();
			p = new PartidoNuevoParser(279);
			p.actualizar();
						
			Thread.sleep(60000);
				
		} while(true);

	}

	public static String convertirATexto(String path) {

		String texto = new String();

		try {

			InputStream is = new FileInputStream(path);
			EditorKit ek = new HTMLEditorKit();

			HTMLDocument doc = (HTMLDocument) ek.createDefaultDocument();
			doc.putProperty("IgnoreCharsetDirective", true);

			InputStreamReader isr = new InputStreamReader(is);
			System.out.println("Encoding: " + isr.getEncoding());
			ek.read(new BufferedReader(isr), doc, 0);

			texto = doc.getText(0, doc.getLength());

		} catch (IOException e) {
			e.printStackTrace();
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return texto;

	}

	public static void escribirEnFichero(String texto) {
		FileWriter fichero = null;
		PrintWriter pw = null;
		String str;
		try {
			fichero = new FileWriter(
					"C:\\Users\\Pablo\\Desktop\\supermanager\\bases.txt");
			pw = new PrintWriter(fichero);

			BufferedReader reader = new BufferedReader(new StringReader(texto));

			/*
			 * while ((str = reader.readLine()) != null) {
			 * 
			 * if (str.length()>0) if (str.charAt(0) != ' ') pw.println(str); }
			 */
			for (int i = 0; i < bases.length; i++) {
				if (bases[i] == null)
					break;
				pw.println(bases[i].getCodigo());
				pw.println(bases[i].getNombre());
				pw.println(bases[i].getEquipo());
				pw.println(bases[i].getGanados());
				pw.println(bases[i].getMedia());
				pw.println(bases[i].getPrecio());
				pw.println(bases[i].getTiempo());
				pw.println(bases[i].getVal_ultima());
				pw.println(bases[i].getVal_3());
				pw.println(bases[i].getBaja15());
				pw.println(bases[i].getSe_mantiene());
				pw.println(bases[i].getSube15());
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				// Nuevamente aprovechamos el finally para
				// asegurarnos que se cierra el fichero.
				if (null != fichero)
					fichero.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

}